﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WarehouseManagement.BusinessLogic.DTO;
using WarehouseManagement.Presenters.Merchandise;
using WarehouseManagement.ViewModels.Merchandise;
using System.Linq;

namespace WarehouseManagement.Views.Merchandise
{
    public partial class NewEditMerchandise : XtraForm, IView<NewEditMerchandisePresenter>, INewEditMerchandiseViewModel
    {
        NewEditMerchandisePresenter _presenter;
        public delegate void MerchandiseSavedHandler(bool success, bool isEdit);
        public MerchandiseSavedHandler OnMerchandiseSaved = null;

        public NewEditMerchandise()
        {
            InitializeComponent();
            Id = -1;
            SetupPresenter();
            LoadCategoryList();
        }

        public NewEditMerchandisePresenter Presenter
        {
            get
            {
                return _presenter;
            }
        }

        public NewEditMerchandisePresenter SetupPresenter()
        {
            _presenter = new NewEditMerchandisePresenter();
            _presenter.RegisterViewModel(this);
            return _presenter;
        }

        public int Id
        {
            get
            {
                return int.Parse(this.Tag.ToString());
            }
            set
            {
                this.Tag = value;
            }
        }

        public string MerchandiseName
        {
            get
            {
                return txtName.Text;
            }
            set
            {
                txtName.Text = value;
            }
        }

        public string Unit
        {
            get
            {
                return txtUnit.Text;
            }
            set
            {
                txtUnit.Text = value;
            }
        }

        public string TradeMark
        {
            get
            {
                return txtTradeMark.Text;
            }
            set
            {
                txtTradeMark.Text = value;
            }
        }

        public string Madein
        {
            get
            {
                return txtMadein.Text;
            }
            set
            {
                txtMadein.Text = value;
            }
        }

        private bool _isEdit;
        public bool IsEdit
        {
            get { return _isEdit; }
            set
            {
                _isEdit = value;
                if (_isEdit)
                    btnAdd.Text = "Sửa";
                else
                    btnAdd.Text = "Thêm";
            }
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {

            Presenter.Save();

        }

        public void MerchandiseSaved(bool success, bool isEdit)
        {
            if (OnMerchandiseSaved != null)
                OnMerchandiseSaved(success, IsEdit);
            if (success)
                this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void NewEditMerchandise_Load(object sender, EventArgs e)
        {

        }

        private void LoadCategoryList()
        {
            _presenter.LoadCategoryList();
            CategoryId = -1;
        }



        public IEnumerable<MerchandiseCategoryDTO> Categories
        {
            get
            {
                return cboCategory.Tag as IEnumerable<MerchandiseCategoryDTO>;
            }
            set
            {
                var list = value.ToList();
                list.Add(new MerchandiseCategoryDTO() { Id = -1, MerchandiseCategoryName = "Không chọn" });
                cboCategory.Tag = list;

                cboCategory.Properties.Items.Clear();
                foreach (var item in list)
                {
                    cboCategory.Properties.Items.Add(item.MerchandiseCategoryName);
                }


            }
        }


        public int? CategoryId
        {
            get
            {
                if (cboCategory.SelectedIndex == -1) return null;
                var list = cboCategory.Tag as IEnumerable<MerchandiseCategoryDTO>;
                var item = list.ElementAt(cboCategory.SelectedIndex);
                return item.Id;
            }
            set
            {
                var index = FindCategoryInCombo(value ?? -1);

                cboCategory.SelectedIndex = index;
            }
        }

        private int FindCategoryInCombo(int cateId)
        {
            var list = cboCategory.Tag as IEnumerable<MerchandiseCategoryDTO>;
            for (var i = 0; i < list.Count(); i++)
            {
                var item = list.ElementAt(i);
                if (item.Id.Equals(cateId))
                {
                    return i;
                }
            }
            return -1;
        }

        private void cboCategory_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                var frmEditCategory = new NewEditMerchandiseCategory(false);
                frmEditCategory.OnOperationCompleted = new NewEditMerchandiseCategory.OperationCompleted(AddEditCategory_Completed);
                frmEditCategory.ShowDialog();
            }
            else if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Ellipsis)
            {
                var frmEditCategory = new NewEditMerchandiseCategory(true);
                frmEditCategory.OnOperationCompleted = new NewEditMerchandiseCategory.OperationCompleted(AddEditCategory_Completed);

                var list = (cboCategory.Tag as List<MerchandiseCategoryDTO>);

                var selectedObj = list.ElementAt(cboCategory.SelectedIndex);
                if (selectedObj.Id != -1)
                {
                    frmEditCategory.Id = selectedObj.Id;
                    frmEditCategory.CategoryName = selectedObj.MerchandiseCategoryName;
                    frmEditCategory.Notes = selectedObj.Notes;


                    frmEditCategory.ShowDialog();
                }
            }

        }

        private void AddEditCategory_Completed(bool success, bool isEditMode)
        {
            LoadCategoryList();
        }
    }
}